home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / anyref.h next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  5.0 KB  |  147 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef MCOP_ANYREF_H
  24. #define MCOP_ANYREF_H
  25.  
  26. #include "buffer.h"
  27. #include <string>
  28. #include "arts_export.h"
  29. /*
  30.  * BC - Status (2002-03-08): AnyRefBase, AnyRef, AnyConstRef
  31.  *
  32.  * These classes will be kept binary compatibile. To change it, adding a new
  33.  * representation is necessary. No private d pointer for this reason.
  34.  */
  35.  
  36. namespace Arts {
  37.  
  38. class Any;
  39. class ARTS_EXPORT AnyRefBase {
  40. protected:
  41.     void *data;
  42.  
  43.     /*
  44.      * This is used for specifying which is the content of a sequence or
  45.      * an enum or similar. For normal types, it remains unset, as their
  46.      * name can be generated from rep.
  47.      */
  48.     std::string _type;
  49.  
  50.     /*
  51.      * What representation data is pointing to?
  52.      *
  53.      * repInt (int *), repDouble (double *) and repConstChar (const char *)
  54.      * are no native MCOP types, but alternative ways or representing the
  55.      * native "int", "float" and "const char *" types
  56.      */
  57.  
  58.     // BC: adding new representations and types is possible, however, existing
  59.     //     numbers may not be changed
  60.     enum Representation {
  61.         repVoid = 0,
  62.         repByte = 10,
  63.         repLong = 20, repInt = 21 /* treated as long */,
  64.         repFloat = 30, repDouble = 31 /* treated as float */,
  65.         repString = 40, repCString = 41 /* string */,
  66.         repBool = 50,
  67.         repByteSeq = 510,
  68.         repLongSeq = 520,
  69.         repFloatSeq = 530,
  70.         repStringSeq = 540,
  71.         repBoolSeq = 550,
  72.         repAny = 1000              /* may hold any type */
  73.     } rep;
  74.  
  75.     void _write(Buffer *b) const;
  76.     void _read(Buffer *b) const;
  77.  
  78.     AnyRefBase(const void *data, Representation rep)
  79.         : data(const_cast<void *>(data)), rep(rep) { };
  80.     AnyRefBase(const void *data, Representation rep, const char *type)
  81.         : data(const_cast<void *>(data)), _type(type), rep(rep) { };
  82.     AnyRefBase(const AnyRefBase ©)
  83.         : data(copy.data), _type(copy._type), rep(copy.rep) { }
  84. public:
  85.     std::string type() const;
  86. };
  87.  
  88. class ARTS_EXPORT AnyConstRef : public AnyRefBase {
  89. public:
  90.     AnyConstRef()                          : AnyRefBase(0,repVoid) { };
  91.     AnyConstRef(const mcopbyte& value)    : AnyRefBase(&value,repByte) { };
  92.     AnyConstRef(const int&     value)        : AnyRefBase(&value,repInt) { };
  93.     AnyConstRef(const long&     value)    : AnyRefBase(&value,repLong) { };
  94.     AnyConstRef(const float&     value)    : AnyRefBase(&value,repFloat) { };
  95.     AnyConstRef(const double&     value)    : AnyRefBase(&value,repDouble) { };
  96.     AnyConstRef(const std::string& value) : AnyRefBase(&value,repString) { };
  97.     AnyConstRef(const char *value)        : AnyRefBase(value,repCString) { };
  98.     AnyConstRef(const bool&     value)    : AnyRefBase(&value,repBool) { };
  99.  
  100.     AnyConstRef(const std::vector<mcopbyte>& v)
  101.                                         : AnyRefBase(&v,repByteSeq) { };
  102.     AnyConstRef(const std::vector<long>& v)    
  103.                                         : AnyRefBase(&v,repLongSeq) { };
  104.     AnyConstRef(const std::vector<float>& v)
  105.                                         : AnyRefBase(&v,repFloatSeq) { };
  106.     AnyConstRef(const std::vector<std::string>& v)
  107.                                         : AnyRefBase(&v,repStringSeq) { };
  108.     AnyConstRef(const std::vector<bool>& v)
  109.                                         : AnyRefBase(&v,repBoolSeq) { };
  110.  
  111.     AnyConstRef(const Any& value)        : AnyRefBase(&value,repAny) { };
  112.  
  113.     AnyConstRef(const AnyConstRef& ref) : AnyRefBase(ref) { }
  114.     void write(Buffer *b) const            { _write(b); }
  115. };
  116.  
  117. class ARTS_EXPORT AnyRef : public AnyRefBase {
  118. public:
  119.     AnyRef()                              : AnyRefBase(0,repVoid) { };
  120.  
  121.     // primitive types
  122.     AnyRef(mcopbyte& value)                : AnyRefBase(&value,repByte) { };
  123.     AnyRef(int&     value)                : AnyRefBase(&value,repInt) { };
  124.     AnyRef(long&     value)                : AnyRefBase(&value,repLong) { };
  125.     AnyRef(float&     value)                : AnyRefBase(&value,repFloat) { };
  126.     AnyRef(double&     value)                : AnyRefBase(&value,repDouble) { };
  127.     AnyRef(std::string&    value)            : AnyRefBase(&value,repString) { };
  128.     AnyRef(bool&     value)                : AnyRefBase(&value,repBool) { };
  129.  
  130.     // sequence of primitive types
  131.     AnyRef(std::vector<mcopbyte>& value)    : AnyRefBase(&value,repByteSeq) { };
  132.     AnyRef(std::vector<long>& value)        : AnyRefBase(&value,repLongSeq) { };
  133.     AnyRef(std::vector<float>& value)        : AnyRefBase(&value,repFloatSeq) { };
  134.     AnyRef(std::vector<std::string>& value)    : AnyRefBase(&value,repStringSeq){};
  135.     AnyRef(std::vector<bool>& value)    : AnyRefBase(&value,repBoolSeq){};
  136.  
  137.     AnyRef(Any& value)                        : AnyRefBase(&value,repAny) { };
  138.  
  139.     AnyRef(const AnyRef& ref) : AnyRefBase(ref) { }
  140.  
  141.     void read(Buffer *b) const            { _read(b);  }
  142.     void write(Buffer *b) const            { _write(b); }
  143. };
  144.  
  145. }
  146. #endif /* MCOP_ANYREF_H */
  147.